<?php
namespace app\controllers;

use app\common\components\BaseWebController;
use app\common\service\applog\AppLogService;
use app\common\service\UtilService;
use yii\log\FileTarget;

class ErrorController extends BaseWebController
{
//    public $layout = false;
    public function actionError()
    {
        $error = \Yii::$app->errorHandler->exception;

        if( $error ){
            $message  = $error->getMessage();
            $file     = $error->getFile();
            $line     = $error->getLine();
            $code     = $error->getCode();


            $log = new FileTarget();
            $log->logFile = \Yii::$app->getRuntimePath().'/logs/err.log';
            $err_msg = $message."[file:{$file}][line:{$line}][code:{$code}][url:{$_SERVER['REQUEST_URI']}][POST_DATA:".http_build_query( $_POST )."]";
            $log->messages[] = [
                $err_msg,
                1,
                'application',
                microtime( true )
            ];

            $log->export();

            //todo 写入到数据库
            AppLogService::addErrorLog( \Yii::$app->id,$err_msg );
        }



        return $this->render('error',['err_msg'=>$err_msg]);
    }
}